<template>
	<view class="content">
		<view class="child-content">
			<view class="title">添加孩子</view>
			<input class="input-childid" v-model="identifier" type="number" placeholder="请输入孩子的账户ID" placeholder-style="color:#C0C4CC;" />
			<view class="cont" v-if="child.identifier" @click="selChild =! selChild">
				<view class="children-left">
					<headimg :realSrc="child.avatar"></headimg>
					<view class="childmsg">
						<view class="stumsg">{{xingshi+'*'+ming}}</view>
						<view class="schoolmsg">{{child.childmsg}}</view>
					</view>
				</view>
				<view class="radio-stu">
					<radio value="child" color="#FF9900" :checked="selChild" />
				</view>
			</view>
		</view>
		<view class="button" @click="changeShowCheck(true)" v-if="selChild">确认选择</view>
		<view class="button" @click="childList" v-else>确认</view>
		<view class="mengban" @click="changeShowCheck(false)" v-if="showCheck"></view>
		<view v-if="showCheck" class="checkChild">
			<view class="iconfont iconguanbi" @click="changeShowCheck(false)"></view>
			<view class="check-title">身份信息验证</view>
			<view class="name-content">
				<view class="xing-shi">{{xingshi}}</view>
				<view class="check-ming">
					<input maxlength="1" type="text" num v-model="checkMing" />
				</view>
				<view class="ming">{{ming}}</view>
			</view>
			<view class="placholder">请输入学生全名</view>
			<view class="check-btn" @click="bind">确认</view>
		</view>
	</view>
</template>

<script>
	import util from '@/common/util.js';
	import headimg from '@/components/head_img.vue';
	export default {
		components: {
			headimg
		},
		data() {
			return {
				identifier: '',
				child: {},
				selChild: false,
				showCheck: false,
				xingshi: '',
				checkMing: '',
				ming: ''
			}
		},
		methods: {
			/* 搜索 */
			search(e) {
				this.identifier = e.target.value;
				this.childList();
			},
			// 
			changeShowCheck(bol) {
				this.checkMing = ''
				this.showCheck = bol
			},
			/* 搜索列表 */
			childList() {
				if (!this.identifier) {
					uni.showToast({
						title: '请输入账号ID',
						icon: 'none'
					})
					return
				}
				var data = {
					identifier: this.identifier
				}
				this.$zapi.apiPost('user/currency.Parents/ChildrenSearch', data).then(res => {
					if (res.data.code == 200) {
						this.child = res.data.data;
						if (this.child.school_info && this.child.school_info.length > 0 && this.child.school_info[0].schoolid > 0) {
							let school = this.child.school_info[0]
							this.child.gradeName = school.grade ? util.getgradeName(school.grade) : '未知年级';
							this.child.school_name = school.school_name ? school.school_name : '未知学校';
							this.child.childmsg = school.class_name ? this.child.school_name + '（' + this.child.gradeName +
								school.class_name + '）' : this.child.school_name + '（未加入班级）';
						} else {
							this.child.childmsg = '未绑定学校';
						}

						let nicheng = this.child.nicheng;
						if (nicheng.length == 2) {
							this.xingshi = nicheng.substring(0, 1)
							this.ming = ''
						} else if (nicheng.length == 3) {
							this.xingshi = nicheng.substring(0, 1)
							this.ming = nicheng.substring(2)
						} else if (nicheng.length > 3) {
							this.xingshi = nicheng.substring(0, 2)
							this.ming = nicheng.substring(3)
						} else {
							this.xingshi = ''
							this.ming = ''
						}
					} else {
						this.child = {}
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					}
				})
			},
			/* 绑定孩子 */
			bind() {
				if (this.checkMing) {
					if (this.child.nicheng.indexOf(this.checkMing) > -1) {
						var data = {
							identifier: this.identifier
						}
						this.$zapi.apiPost('user/currency.Parents/BindingChildren', data).then(res => {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
							this.showCheck = false
							this.checkMing = ''
						})
					} else {
						uni.showToast({
							title: '验证失败，请正确输入该学生全名',
							icon: 'none'
						})
						this.checkMing = ''
					}
				} else {
					uni.showToast({
						title: '请输入学生全名',
						icon: 'none'
					})
					this.checkMing = ''
				}

			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FAFAFA;
	}

	.content {
		width: 100%;
		box-sizing: border-box;

		.child-content {
			padding: 30upx 30upx 0 30upx;
			box-sizing: border-box;

			.title {
				color: #333333;
				font-size: 28upx;
				font-weight: 400;
			}

			.input-childid {
				width: 100%;
				height: 90upx;
				line-height: 90upx;
				padding: 0 30upx;
				box-sizing: border-box;
				border: 2upx solid #DCDFE6;
				opacity: 1;
				border-radius: 10upx;
				margin: 30upx 0;
			}
		}

	}

	.input-border {
		width: 100%;
		height: 100upx;
		background: #FFFFFF;
		display: flex;
		flex-direction: row;
		justify-content: center;
	}

	.input-exam {
		width: 650upx;
		height: 60upx;
		line-height: 60upx;
		border-radius: 6upx;
		background: #FAFAFA;
		margin-top: 20upx;
		padding-left: 20upx;
	}

	.cont {
		width: calc(100% - 60upx);
		padding: 40upx 20upx 40upx 40upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-radius: 10upx;
		margin-top: 30upx;
		background-color: #FFFFFF;

		.children-left {
			width: 89%;
			display: flex;

			.head_image {
				width: 80upx;
				height: 80upx;
				border-radius: 50%;
			}

			.childmsg {
				color: #333333;
				margin-left: 20upx;
				width: calc(100% - 100upx);

				.stumsg {
					color: #333333;
					line-height: 20px;
					font-weight: bold;
					font-family: Source Han Sans CN;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}

				.schoolmsg {
					font-family: Source Han Sans CN;
					line-height: 18px;
					color: #666666;
					font-size: 24upx;
					white-space: pre-wrap;
					word-break: break-word;
				}
			}
		}

		.radio-stu {
			radio {
				transform: scale(0.7);
			}
		}
	}

	.button {
		position: absolute;
		width: 100%;
		bottom: 0;
		background: #FF9900;
		height: 100upx;
		line-height: 100upx;
		text-align: center;
		color: #FFFFFF;
		font-size: 36upx;
	}

	.mengban {
		position: fixed;
		top: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.21);
	}

	.checkChild {
		width: 440upx;
		height: 310upx;
		box-sizing: border-box;
		background-color: #FFFFFF;
		box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.12);
		opacity: 1;
		border-radius: 5px;
		position: absolute;
		top: 50%;
		left: 50%;
		margin-top: -155upx;
		margin-left: -220upx;

		view {
			text-align: center;
		}

		.iconguanbi {
			width: 100%;
			text-align: right;
			color: #C0C4CC;
			padding: 10upx;
			box-sizing: border-box;
			font-size: 24upx;
		}

		.check-title {
			width: 100%;
			color: #333333;
			font-size: 24upx;
			font-weight: 400;
		}

		.name-content {
			width: 100%;
			height: 54upx;
			overflow: hidden;
			text-overflow: ellipsis;
			display: flex;
			justify-content: center;
			margin: 24upx 0;

			.xing-shi,
			.ming {
				color: #333333;
				font-size: 40upx;
				font-weight: 400;
			}

			.check-ming {
				width: 50upx;
				height: 50upx;
				border: 2upx solid #DCDFE6;
				margin: 0 10upx;
			}
		}

		.placholder {
			font-size: 20upx;
			color: #C0C4CC;
		}

		.check-btn {
			width: 124upx;
			height: 40upx;
			text-align: center;
			color: #FFFFFF;
			background-color: #FF9900;
			margin: auto;
			margin-top: 24upx;
			border-radius: 40upx;
		}
	}
</style>
